package com.snifferx.util.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.log4j.Logger;

public class PropertiesManager {
	private static Logger log = Logger.getLogger(PropertiesManager.class);
	protected static String deployTargetPropertiesFile = "DeployTarget.properties";
	public static String PROPERTIES_PATH;
	protected static String eapplnPropertiesFile;
	protected static String smeEapplnPropertiesFile;
	protected static String eshopSSOPropertiesFile;
	protected static Properties deployTargetProperties;
	protected static Properties eapplnProperties;
	protected static Properties eshopSSOProperties;
	protected static PropertiesManager pm;

	public static final String JAVA_NAMING_FACTORY_INITIAL = "java.naming.factory.initial";
	public static final String JAVA_NAMING_PROVIDER_URL = "java.naming.provider.url";
	public static final String JAVA_NAMING_SECURITY_PRINCIPAL = "java.naming.security.principal";
	public static final String JAVA_NAMING_SECURITY_CREDENTIALS = "java.naming.security.credentials";
	public static final String ORDER_UPLOADER_PROVIDER_URL = "orderUploader.provider.url";
	public static final String FULFILLMENT_SYSTEM_PROVIDER_URL = "fulfillmentSystem.provider.url";
	public static final String CONSOLEHANDLER_LEVEL = "CONSOLEHANDLER_LEVEL";
	public static final String UPLOAD_IMAGE_PATH = "UPLOAD_IMAGE_PATH";

	static {
		if (isWindows()) {
			PROPERTIES_PATH = "E:\\Important!\\";
			System.out.println(">> Deployment environment is window . changed PROPERTIES_PATH succeeded !");
		} else {
			PROPERTIES_PATH = "/usr01/webapp/properties/";
			System.out.println(">> Deployment environment is Linux . Keep default PROPERTIES_PATH !");
		}
		eapplnPropertiesFile = PROPERTIES_PATH + "eappln.properties";
		smeEapplnPropertiesFile = PROPERTIES_PATH + "sme_eappln.properties";
		eshopSSOPropertiesFile = PROPERTIES_PATH + "eshop_sso.properties";
		loadEapplnProperties();
	}

	protected PropertiesManager() {
		deployTargetProperties = new Properties();
		loadDeployTargetProperties();
	}

	private void loadDeployTargetProperties() {

		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(deployTargetPropertiesFile);
		if (is == null)
			log.debug("Properties file NOT loaded: " + deployTargetPropertiesFile);
		else
			log.debug("PropertiesManager - Loaded properties file: " + deployTargetPropertiesFile);
		if (is != null) {
			try {
				deployTargetProperties.load(is);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static String getDeployTargetPropertyAsString(String key) {
		String result = null;
		result = deployTargetProperties.getProperty(key);
		if (result == null) {
			result = deployTargetProperties.getProperty(key);
		}
		return result;
	}

	private static void loadEapplnProperties() {

		InputStreamReader in = null;
		eapplnProperties = new Properties();
		try {
			log.debug("Properties file being loaded: " + eapplnPropertiesFile);

			in = new InputStreamReader(new FileInputStream(eapplnPropertiesFile), "UTF-8");
			if (in != null)
				eapplnProperties.load(in);
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("Properties file not loaded: " + eapplnPropertiesFile);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}

	}

	public static String getEapplnPropertyAsString(String key) {
		String result = null;
		result = eapplnProperties.getProperty(key);
		if (result == null) {
			result = eapplnProperties.getProperty(key);
		}
		return result;
	}

	public static Properties loadPropertiesFile(String filename) {

		InputStreamReader in = null;
		// Properties prop = new Properties();
		eapplnProperties = new Properties();
		try {
			in = new InputStreamReader(new FileInputStream(filename), "UTF-8");
			if (in != null)
				eapplnProperties.load(in);

		} catch (Exception e) {
			e.printStackTrace();
			log.warn("Properties file not loaded: " + filename);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}

		return eapplnProperties;

	}

	public static boolean isWindows() {
		boolean flag = false;
		if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
			flag = true;
			System.out.println("System is Windows");
		} else {
			System.out.println("System is Linux.");

		}
		return flag;
	}

}
